import router from "@/router/index";
export function cPageData({ propData, emit, formEmitsObj, dataed }: { dataed: any; propData: CSuperFormPropsDataType; emit: (event: string, ...args: any[]) => void; formEmitsObj: any }) {
  return {
    id: router.currentRoute.value.params.id,
    emit: emit,
    propData: propData,
    cPageOpt: propData.cPageOpt,
    formParams: propData.formParams,
    isSaving: dataed.value?.isSaving || false,
    formEmitsObj
  };
}
export function cPageFn(data: any) {
  const methods = {
    backToList: () => {
      router.back();
    },
    judgeAdd() {
      if (data.value.id === "0") {
        if (data.value.props.formParams) data.value.cFormRef.value?.initFormRef(data.value.propData.formParams);
      } else {
        this.getDetail();
      }
    },
    //获取详情
    getDetail: async () => {
      if (!data.value.cPageOpt?.detailUrl) return;
      const res = await hfoRequest.get({ url: data.value.cPageOpt.detailUrl + `/${data.value.id}` });
      if (res) {
        data.value.emit("getDetailHandle", res.data);
        const obj = res.data;
        data.value.cFormRef.value?.initFormRef(obj);
        if (!data.value.form.value["id"] && obj.id) data.value.form.value["id"] = obj.id;
      }
    },
    submitForm() {
      try {
        data.value.isSaving = true;
        const res = data.value.cFormRef.value?.submitForm();
        data.value.emit("saveAfter", {
          res: res,
          fn: () => {
            return router.back();
          }
        });
        data.value.isSaving = false;
      } catch (error) {
        data.value.isSaving = false;
      }
    },
    initialFn() {
      data.value.emit("changeProps", { e: true, propsName: ["cformOpt", "otherBtn"] });
    }
  };
  methods.initialFn();
  methods.judgeAdd();
  return methods;
}
